package com.ruoyi.video.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.video.mapper.VideoLineItemMapper;
import com.ruoyi.video.domain.VideoLineItem;
import com.ruoyi.video.service.IVideoLineItemService;

/**
 * 视频文案Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-06
 */
@Service
public class VideoLineItemServiceImpl implements IVideoLineItemService 
{
    @Autowired
    private VideoLineItemMapper videoLineItemMapper;

    /**
     * 查询视频文案
     * 
     * @param id 视频文案主键
     * @return 视频文案
     */
    @Override
    public VideoLineItem selectVideoLineItemById(Long id)
    {
        return videoLineItemMapper.selectVideoLineItemById(id);
    }

    /**
     * 查询视频文案列表
     * 
     * @param videoLineItem 视频文案
     * @return 视频文案
     */
    @Override
    public List<VideoLineItem> selectVideoLineItemList(VideoLineItem videoLineItem)
    {
        return videoLineItemMapper.selectVideoLineItemList(videoLineItem);
    }

    @Override
    public List<VideoLineItem> selectVideoLineItemListGroupByList(Long videoId, String language) {
        return videoLineItemMapper.selectVideoLineItemListGroupByList(videoId, language);
    }

    /**
     * 新增视频文案
     * 
     * @param videoLineItem 视频文案
     * @return 结果
     */
    @Override
    public int insertVideoLineItem(VideoLineItem videoLineItem)
    {
        return videoLineItemMapper.insertVideoLineItem(videoLineItem);
    }

    /**
     * 修改视频文案
     * 
     * @param videoLineItem 视频文案
     * @return 结果
     */
    @Override
    public int updateVideoLineItem(VideoLineItem videoLineItem)
    {
        return videoLineItemMapper.updateVideoLineItem(videoLineItem);
    }

    /**
     * 批量删除视频文案
     * 
     * @param ids 需要删除的视频文案主键
     * @return 结果
     */
    @Override
    public int deleteVideoLineItemByIds(Long[] ids)
    {
        return videoLineItemMapper.deleteVideoLineItemByIds(ids);
    }

    /**
     * 删除视频文案信息
     * 
     * @param id 视频文案主键
     * @return 结果
     */
    @Override
    public int deleteVideoLineItemById(Long id)
    {
        return videoLineItemMapper.deleteVideoLineItemById(id);
    }

    @Override
    public int deleteVideoLineItemByUser(Long userId, Long videoId) {
        return videoLineItemMapper.deleteVideoLineItemByUserAndVideo(userId, videoId);
    }

    @Override
    public int resetVideoLine(Long userId, Long videoId, JSONArray list) {
        videoLineItemMapper.deleteVideoLineItemByUserAndVideo(userId,videoId);
        List<VideoLineItem> lineItems = list.stream()
                .map(jsonObject -> new VideoLineItem(userId, videoId, JSONObject.from(jsonObject)))
                .collect(Collectors.toList());
        return videoLineItemMapper.insertVideoLineItemList(lineItems);
    }

    @Override
    public int resetVideoLine(Long userId, List<VideoLineItem> list) {
        list.forEach(item -> item.setUserId(userId));
        return videoLineItemMapper.insertVideoLineItemList(list);
    }
}
